package com.eascs.crm.nsEditDetail.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.Date;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
//import com.eascs.app.log.Logger;
//import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.crm.nsEditDetail.entity.CarInfoEntity;
import com.eascs.crm.nsEditDetail.entity.CarInfoLogEntity;
import com.eascs.crm.nsEditDetail.form.CarInfoForm;
import com.eascs.crm.nsEditDetail.form.CarLogInfoForm;
import com.eascs.crm.nsEditDetail.service.CarInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.web.sso.UserHolder;

/**
 * 夫妻双方车产信息
 * 
 * @author lwx
 *
 */
@Controller
@RequestMapping("/car")
public class CarInfoRestController extends BaseRestController {
//	private static Logger logger = LoggerFactory.getLogger(CarInfoRestController.class);

	@Inject
	CarInfoService  carInfoService;
	@Autowired
	O2oLoanapplyService o2oLoanapplyService;

	@RequestMapping("/car_list")
	public PaginationRestResult car_list(String loanapplyId,@RequestParam(defaultValue="0") Integer showPage, String type) {
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyId);
		PageResult<Object[]> pageResult = carInfoService.findCarListInfo(loanapplyId,showPage);
		String[] oatitles = { "品牌", "购入价值", "购入时间", "是否按揭","估值方法", "信贷经理估值" };
		String o2ourl = "/car/car_list";
		if (StringUtil.isNotBlank(loanapplyId)) {
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanapplyId", loanapplyId);
			o2ourl = o2ourl + "&showPage=";
		} else {
			o2ourl = o2ourl + "?showPage=";
		}
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, oatitles, o2ourl);
		Button editButton = null;
		Button delButton = null;
		if (!"detail".equals(type)) {
			if(StatusContexts.O2O_ZX_END.equals(o2oLoanapplyEntity.getWfType())){
				editButton = ButtonHelper.createAjaxButton("修改", "/car/car_op?carId=", "UpdateInternalInfo");
				delButton = ButtonHelper.createAjaxButton("  删除 ", "/car/car_del?carId=", "UpdateInternalInfo");
			}
		}
		Button[] detailButton = new Button[] { editButton, delButton };
		SelectDataOption[] selectDataOptions = {null,null,null,new SelectDataOption("SFAJ"),new SelectDataOption("CCGZFF"), null,null};
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setMoreButtons(detailButton);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setRendTo("#o2o_car");
		paginationInfo.setFormats(new Format[] { null, null, null, null, null, new DecimalFormat(",##0.00") });
		ViewContext.put("paginationInfo", paginationInfo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	// 新增修改
	@RequestMapping(value = "/car_op")
	public HtmlRestResult carOp(String loanapplyId,String carId) {
		CarInfoEntity vo = new CarInfoEntity();
		if (StringUtil.isNotBlank(carId)) {
			vo = carInfoService.findCarInfoEntity(carId);
		}
		ViewContext.put("loanapplyId", loanapplyId);
		ViewContext.put("vo", vo);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/car/car_op");
	}

	@RequestMapping(value = "/car_del")
	public BaseRestResult<String> carDel(String carId) {
		try {
			int i = carInfoService.carDel(carId);
			if(i>0){
				return BaseRestResult.info("删除成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_car");
			}else{
				return BaseRestResult.info("删除失败").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_car");
			}
		} catch (Exception e) {
			return BaseRestResult.warn("数据库操作异常");
		}
	
	}

	@RequestMapping("/car_save")
	public BaseRestResult<String> carSave(CarInfoForm form) {
		int flag = 0;
		if (form != null && StringUtil.isNotBlank(form.getCarId())) {// 修改
			CarInfoEntity car = carInfoService.findCarInfoEntity(form.getCarId());
			try {
				flag = carInfoService.carUpdate(car, form);
			} catch (Exception e) {
				return BaseRestResult.warn("数据库操作异常");
			}
		} else {// 新增
			CarInfoEntity car = new CarInfoEntity();
			try {
				flag =carInfoService.carSave(car, form);
			} catch (Exception e) {
				return BaseRestResult.warn("数据库操作异常");
			}
		}
		if(flag>0){
			return BaseRestResult.info("成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_car");
		}else{
			return BaseRestResult.info("操作失败").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_car");
		}
	}
	
	@RequestMapping("/car_list_form")
	public PaginationRestResult car_list_form(String loanapplyId,@RequestParam(defaultValue="0") Integer showPage) {
		PageResult<Object[]> pageResult = carInfoService.findCarListInfo(loanapplyId,showPage);
		String[] oatitles = { "品牌", "购入价值", "购入时间", "是否按揭","估值方法", "信贷经理估值", "估值复核（万元）" };
		String o2ourl = "/car/car_list_form";
		if (StringUtil.isNotBlank(loanapplyId)) {
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanapplyId", loanapplyId);
			o2ourl = o2ourl + "&showPage=";
		} else {
			o2ourl = o2ourl + "?showPage=";
		}
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, oatitles, o2ourl);
		AjaxButton editButton = new AjaxButton("修改", "/car/ns_car_view?carId=");
		Button[] detailButton = new Button[] { editButton };
		SelectDataOption[] selectDataOptions = {null,null,null,new SelectDataOption("SFAJ"),new SelectDataOption("CCGZFF"), null,null};
		paginationInfo.setSelectDataOptions(selectDataOptions);
		paginationInfo.setMoreButtons(detailButton);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setRendTo("#o2o_car_form");
		paginationInfo.setFormats(new Format[] { null, null, null, null, null, null });
		ViewContext.put("paginationInfo", paginationInfo);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}

	
	@RequestMapping(value = "/ns_car_view")
	public HtmlRestResult nsCarView(String carId) {
		CarInfoEntity vo = new CarInfoEntity();
		CarInfoLogEntity log =new CarInfoLogEntity();
		if (StringUtil.isNotBlank(carId)) {
			vo = carInfoService.findCarInfoEntity(carId);
			log = carInfoService.findCarLogEntity(carId);
		}
		ViewContext.put("vo", vo);
		ViewContext.put("log", log);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/car/car_op_form");
	}
	
	@RequestMapping("/car_form_save")
	public BaseRestResult<String> carFormSave(CarLogInfoForm form) {
		CarInfoLogEntity log = new CarInfoLogEntity();
		log.setCarInfoId(form.getCarId());
		log.setOpPsn(UserHolder.getUser().getCname());
		log.setOpNo(UserHolder.getUser().getCode());
		log.setOpDate(new Date());
		log.setValuation(form.getValuation());
		log.setCarPrice(form.getCarPriceReview());
		int i = carInfoService.insert(log);
		if(i>0){
			return BaseRestResult.info("成功").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_car_form");
		}else{
			return BaseRestResult.warn("操作失败").andCloseActionDialog().andJquerySelectAjaxAction("#o2o_car_form");
		}
	}
	
}
